package com.dfch.auto.appium.utils;

import com.alibaba.fastjson.JSON;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.Map;

public class EnvUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(EnvUtils.class);

    /**
     * 判断环境变量是否有配置
     *
     * @param env
     * @return
     */
    public static boolean hasEnv(String... env) {
        Map<String, String> map = System.getenv();
        LOGGER.debug("环境变量:{}", JSON.toJSONString(map));
        for (String e : env) {

            if (e.startsWith("%")) {
                e = map.get(e.split("%")[1]) + e.split("%")[2];
            }
            String path = map.get("Path");

            LOGGER.debug("Path:{}", path);
            if (StringUtils.isEmpty(path)) {
                throw new RuntimeException("环境变量获取失败:path not found!");
            }
            if (!map.containsKey(e) && (StringUtils.isEmpty(path) || path.indexOf(e) == -1)) {
                throw new RuntimeException("环境变量没有配置:" + e);
            }
        }
        return false;
    }

    /**
     * 获取环境变量
     * @param key
     * @return
     */
    public static String getEnv(String key) {
        Map<String, String> map = System.getenv();

        return map.get(key);
    }
}
